// pages/zhifuye/zhifuye.js
const app = getApp();
import {
  areaList
} from '@vant/area-data';
Page({

  /**
   * 页面的初始数据
   */
  data: {

    zongsuju: '', //详情数据
    SALES_TYPE: '', //判断是否购买
    GOODS_CLASSIFICATION: '1', //判断是否是虚拟商品
    CUSTOMER_TEL: '18888888888', //客户手机号
    GOODS_ID: '', //商品单号
    ADDRESS_ID: '', //返回的地址ID
    openid: '',
    MESSAGE: '',
    ORDER_NUMBER: '',
    ORDER_PRICE_ZERO: '',
    prepay_id: '',
    nonceStr: '',
    package: '',
    paySign: '',
    signType: '',
    timeStamp: '',


    CUSTOMER_ID: '', //用户id

    areaList,
    num: 1,
    minusStatus: '1',
    PAYMENT_AMOUNT: '', //变化价格
    jiaaa: '', //初始价格
    CONSIGNEE_ADDRESS: '', //内容
    show: false,
    shengji: '',
    shiji: '',
    xianji: '',
    dizhilan: true,


    L2code: '',
    L3code: '',
    city: [], //市
    district: [], //区
    business: '', //网点
    shi_k: true,
    qu_k: true,
    wang_k: true,
  },


  xiandi() {
    this.setData({
      dizhilan: false
    })
  },

  dizhiding() {

    this.setData({
      openid: app.globalData.openid
    });

    if (this.data.CUSTOMER_TEL != '' || this.data.shengji != '' || this.data.CONSIGNEE_ADDRESS != '') {
      this.setData({
        dizhilan: true
      })
      wx.showToast({
        title: '地址填写成功',
        icon: 'success',
        duration: 2000
      })
    } else {
      this.setData({
        dizhilan: true
      })
      wx.showToast({
        title: '请完善信息',
        icon: 'error',
        duration: 2000
      })

    }



  },


  quxiao() {
    this.setData({
      show: false
    });
  },

  queding(e) {
    console.log(e);
    console.log(e.detail.values[0].name);
    console.log(e.detail.values[1].name);
    console.log(e.detail.values[2].name);
    this.setData({
      show: false,
      shengji: e.detail.values[0].name,
      shiji: e.detail.values[1].name,
      xianji: e.detail.values[2].name,
    });
  },


  inptext(e) {
    this.setData({
      CONSIGNEE_ADDRESS: e.detail.value
    })
  },


  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },







  save() {
    console.log(1111);

    this.setData({
      CUSTOMER_ID: app.globalData.CUSTOMER_ID,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    });



    // if (this.data.CUSTOMER_TEL == '' || this.data.shengji == '' || this.data.CONSIGNEE_ADDRESS == '') {
    //   wx.showToast({
    //     title: '请填写地址',
    //     icon: 'error',
    //     duration: 2000
    //   })
    // } else {
      wx.request({
        url: app.globalData.interface+'/newVersion/API_2071_saveShippingAddress',
        method: 'POST',
        data: {
          CUSTOMER_ID: this.data.CUSTOMER_ID,
          OPEN_ID: this.data.openid,
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
          CUSTOMER_TEL: this.data.CUSTOMER_TEL,
          SHOP_TYPE: this.data.GOODS_CLASSIFICATION,
          PROVINCE_NAME: this.data.shengji,
          CITY_NAME: this.data.shiji,
          DISTRICT_NAME: this.data.xianji,
          ADDRESS: this.data.CONSIGNEE_ADDRESS,
          REMARKS: '',
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data);
          this.setData({
            ADDRESS_ID: res.data.shippingAddress.ADDRESS_ID
          })

          if (res.data.result == 'success') {
            this.pay()
          }
        }
      })
    // }
  },










  pay: function () {


    if (this.data.CUSTOMER_TEL == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'error',
        duration: 2000
      })
    } else {
      console.log(this.data.openid);
      console.log(111);
      // this.gitxin() 
      wx.request({
        url: app.globalData.interface+'/newVersion/API_1070_weiXinPayment',
        method: 'POST',
        data: {
          GOODS_ID: this.data.GOODS_ID,
          ADDRESS_ID: this.data.ADDRESS_ID,
          PAYMENT_AMOUNT: this.data.PAYMENT_AMOUNT,
          OPEN_ID: this.data.openid,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          this.setData({
            MESSAGE: res.data.MESSAGE,
            ORDER_NUMBER: res.data.ORDER_NUMBER,
            ORDER_PRICE_ZERO: res.data.ORDER_PRICE_ZERO,
            prepay_id: res.data.prepay_id,
            nonceStr: res.data.MESSAGE.nonceStr,
            package: res.data.MESSAGE.package,
            paySign: res.data.MESSAGE.paySign,
            signType: res.data.MESSAGE.signType,
            timeStamp: res.data.MESSAGE.timeStamp,
          })
          console.log(res.data.MESSAGE.nonceStr);
          if (!res.data.MESSAGE.nonceStr || !res.data.MESSAGE.package || !res.data.MESSAGE.paySign) {
            wx.showToast({
              title: '付款失败',
              icon: 'loding',
              duration: 2000
            })
          } else {

            wx.requestPayment({
              'timeStamp': this.data.timeStamp,
              'nonceStr': this.data.nonceStr,
              'package': this.data.package,
              'signType': this.data.signType,
              'paySign': this.data.paySign,
              'success': function (res) {
                console.log('支付返回数据', res)

            

                wx.hideLoading({
                  success: (res) => {
                    console.log(res);
                    wx.showToast({
                      title: '付款成功',
                      icon: 'loding',
                      duration: 1500
                    })
                  },
                })
                setTimeout(() => {
                  wx.navigateTo({
                    url: '../../pages/youpiaoqianzi/youpiaoqianzi'
                  })
                }, 1500)


              },
              'fail': function (res) {
                wx.showToast({
                  title: '付款失败',
                  icon: 'error',
                  duration: 2000
                })
                // wx.redirectTo({
                //   url: '../order/order?currentIndex=' + 1 + '&formUrl=' + 1 + '&formOrder=2',
                // })
              },
              // complete: function () {
              //   this.setData({
              //     hiddenLoading: true,
              //     submitOrder: true
              //   })
              // }
            })
            console.log(this.data.timeStamp);
            console.log(this.data.nonceStr);
            console.log(this.data.package);
            console.log(this.data.signType);
            console.log(this.data.paySign);
          }
          // console.log(JSON.stringify(res.data.myPrizeList));
        }

      })

    }


  },









  // 地区数据
  region() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2029_outletPush',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        this.setData({
          city: res.data.data,
        })

      }
    })
  },


  but_shi() {
    this.setData({
      shi_k: false
    })
  },

  but_xian() {

    console.log(111);
    if (this.data.L2code == '') {
      wx.showToast({
        title: '请先选择市',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        qu_k: false
      })
    }
  },

  but_w() {

    if (this.data.L2code == '') {
      wx.showToast({
        title: '请先选择区',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        wang_k: false
      })
    }

  },


  //市
  buttn_c(e) {
    console.log(111);
    console.log(e);
    console.log(e.currentTarget.dataset.item.district);
    console.log(e.currentTarget.dataset.item.L2code);
    this.setData({
      district: e.currentTarget.dataset.item.district,
      L2code: e.currentTarget.dataset.item.L2code,
      L2name: e.currentTarget.dataset.item.L2name,
      shi_k: true

    })

  },

  buttn_x(e) {
    console.log(e);


    this.setData({
      L3code: e.currentTarget.dataset.item.L3code,
      L3name: e.currentTarget.dataset.item.L3name,
      qu_k: true
    })
    this.Outlets()

  },


  buttn_w(e) {
    console.log(e);
    this.setData({
      L4name: e.currentTarget.dataset.item.L4name,
      EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,
      wang_k: true
    })
  },

  //网点
  Outlets() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2027_outletList',
      method: 'POST',
      data: {
        L2code: this.data.L2code,
        L3code: this.data.L3code
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.data);
        this.setData({
          business: res.data.data
        })

      }
    })
  },








  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log(e);
    console.log(JSON.parse(e.data));

    var zongsuju = JSON.parse(e.data)

    this.setData({
      zongsuju: zongsuju,
      SALES_TYPE: zongsuju.SALES_TYPE,
      PAYMENT_AMOUNT: zongsuju.GOODS_PRICE, //价格
      jiaaa: zongsuju.GOODS_PRICE,
      // GOODS_CLASSIFICATION: zongsuju.GOODS_CLASSIFICATION, //判断是否是虚拟商品
      GOODS_ID: zongsuju.GOODS_ID, //商品ID

    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      CUSTOMER_ID: app.globalData.CUSTOMER_ID,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,

    });
    this.region()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})